<template>
  <CustomTableCard 
    footer
    :total="total" 
    :page.sync="listQuery.page" 
    :limit.sync="listQuery.limit" 
    @on-page="getList" 
    title="添加">
    <div slot="header">
      <v-row>
        <v-col cols="1">
          <v-select
            v-model="listQuery.status"
            :items="[{name: '激活', value: 1}, {name: '锁定', value: 2}, {name: '全部', value: ''}]"
            label="状态"
            item-text="name"
            item-value="value"
            dense
            outlined
            hide-details
            @change="getList(1)"
          ></v-select>
        </v-col>
        <v-col cols="1">
          <v-select
            :items="['自由行', '团队']"
            label="信息类型"
            dense
            outlined
            hide-details
          ></v-select>
        </v-col>
        <v-col cols="2">
          <v-text-field
            v-model="listQuery.keyword"
            label="关键字搜索"
            outlined
            dense
            hide-details
            clearable
          ></v-text-field>
        </v-col>
        <v-col>
          <v-btn color="info" class="mr-3" @click="getList(1)">{{ $t('actions.search') }}</v-btn>
        </v-col>
        <v-col cols="1" class="text-right">
          <v-btn color="primary" @click="showDrawer = true">{{ $t('actions.add') }}</v-btn>
        </v-col>
      </v-row>
    </div>
    <el-table
      ref="table"
      v-auto-height
      :height="tableHeight"
      :data="tableData"
      stripe
      border
      fit
      @selection-change="handleSelectionChange"
      style="width: 100%">
      <el-table-column
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column label="序号" fixed type="index" align="center" width="60"></el-table-column>
      <el-table-column label="产品名称" fixed show-overflow-tooltip prop="name" width="180"></el-table-column>
      <el-table-column label="信息类型" prop="info_type_name"  width="140"></el-table-column>
      <el-table-column label="始发地" prop="info_type_name" width="140"></el-table-column>
      <el-table-column label="目的地" prop="info_type_name" width="140"></el-table-column>
      <el-table-column label="天数" prop="info_type_name" width="140"></el-table-column>
      <el-table-column label="所属员工" prop="info_type_name" width="140"></el-table-column>
      <el-table-column label="所属部门" prop="info_type_name" width="140"></el-table-column>
      <el-table-column label="创建人" prop="info_type_name" width="140"></el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="updated_at"
        label="更新时间"
        width="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        prop="address"
        label="操作"
        align="center"
        width="180">
        <template slot-scope="{ row }">
          <v-btn small color="info" class="mr-2" outlined @click="handleRmove(row)">删除</v-btn>
          <v-btn small color="warning" outlined @click="handleRmove(row)">删除</v-btn>
        </template>
      </el-table-column>
    </el-table>
    <div slot="actions">
      <v-btn small color="success" :disabled="!multipleSelection.length" class="mr-2" @click="setStatusAll(1)">{{ $t('actions.open') }}</v-btn>
      <v-btn small color="warning" :disabled="!multipleSelection.length" @click="setStatusAll(2)">{{ $t('actions.close') }}</v-btn>
    </div>
  </CustomTableCard>
</template>

<script>
import mixin from './mixin'
import { getScenicList, setStatus } from '@/api/scenic'

export default {
  name: 'product',
  mixins: [mixin],
  created() {
    console.log('created')
    this.getList()
  },
  activated() {
    this.getList()
    console.log('activated')
  },
  deactivated() {
    console.log('deactivated')
  },
  data() {
    return {
      tableHeight: 500,
      page: 1,
      listQuery: {
        page: 1,
        limit: 10,
        status: '',
        info_type_id: undefined,
        city_id: undefined,
        keyword: ''
      },
      multipleSelection: [],
      tableData: [],
      total: 0
    }
  },

  methods: {
    getList(page) {
      if (/\d+/.test(page)) {
        this.listQuery.page = page
      }
      getScenicList(this.listQuery).then(response => {
        this.total = response.data.count
        this.tableData = response.data.data
      }).finally(() => {

      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除列表
    handleRmove(row) {
      let ids = []
      if (row) {
        ids = [row.id]
      } else {
        ids = this.multipleSelection.map(item => item.id)
      }
      this.$confirm(this.$t('tips.delete'), this.$t('other.tip'), {
        center: true,
        type: 'warning'
      }).then(() => {
        removeSpOrder({idArr: ids}).then(() => {
          this.$message.success('success')
          this.getList()
        })
      }).catch(() => {})
    },
    setStatusAll(status) {
      let data = this.multipleSelection.map(item => {
        return {
          id: item.id,
          status: status
        }
      })
      this.updateStatus({listData: data})
    },
    setStatus(row, status) {
      this.updateStatus({listData: [{id: row.id, status: status}]})
    },
    updateStatus(params) {
      setStatus(params).then(() => {
        // this.$message({
        //   type: 'success',
        //   message: 'success!'
        // })
        this.getList()
      })
    },
  }
}
</script>
